package com.dlc.felear.xiaoensale.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.dlc.felear.xiaoensale.R;
import com.dlc.felear.xiaoensale.entity.OnceDispEnty;

import java.util.ArrayList;
import java.util.List;


public class OnceHistoryDispAdapter extends BaseAdapter {

    private static final String TAG = "OnceHistoryDispAdapter";
    // 上下文
    private Context context;
    // 数据
    private List<OnceDispEnty> items;

    public OnceHistoryDispAdapter(Context context) {
        this.context = context;
        items = new ArrayList<>();
    }


    public void setItems(OnceDispEnty data) {
        if (data == null) {
            return;
        }
        if (items.size() >= 9) {
            items.remove(0);
        }
        items.add(data);
        notifyDataSetChanged();
    }

    public void clearData() {
        items.clear();
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return items == null ? 0 : items.size();
    }

    @Override
    public OnceDispEnty getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.adapter_once_history_list, null);
            holder.tv_name = convertView.findViewById(R.id.once_tv_num);
            holder.tv_price = convertView.findViewById(R.id.once_tv_price);
            holder.tv_time = convertView.findViewById(R.id.once_tv_time);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        OnceDispEnty temp=items.get(position);
        holder.tv_time.setText(temp.getTime());
        holder.tv_price.setText(temp.getAccount());
        holder.tv_name.setText(temp.getNum());
        return convertView;
    }


    static class ViewHolder {
        TextView tv_name, tv_price, tv_time;
    }

}